package cn.itcast.controller;

import cn.itcast.domain.Msg;
import cn.itcast.domain.StuAndCourse;
import cn.itcast.domain.Student;
import cn.itcast.domain.Teacher;
import cn.itcast.service.StudentService;
import cn.itcast.service.TeacherService;

import org.apache.ibatis.annotations.Param;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {


    @Autowired
    Student student;

    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    @Autowired
    Teacher teacher;

    @Autowired
    Msg msg = null;


    @RequestMapping("/me")
    public String toMe(){
        return "me";
    }

//    @RequestMapping("/end")
//    public String toEnd(){
//        return "end";
//    }

    @RequestMapping("/student")
    public String toStudent(){
        return "student";
    }

    // 学生查询成绩
    @RequestMapping(value = "/studentToCourse")
    @ResponseBody
    public Msg student_main(HttpSession session){
        student = (Student) session.getAttribute("student");
        if (student != null){
            String stuName = student.getSname();
            String stuNo = student.getSno();
            System.out.println(stuNo+stuName);
            // 获取学生的成绩
            List<StuAndCourse> list = studentService.findByIdtoCourse(stuNo);
            msg = Msg.success().add("list",list);
        }
        return msg;
    }

    // 学生个人信息
    @RequestMapping(value = "/studentToMe")
    @ResponseBody
    public Msg studentToMe(HttpSession session){
        student = (Student) session.getAttribute("student");
        String stuNo = student.getSno();
        student = studentService.findById(stuNo);
        if (student != null){
            // 获取学生的信息
            student = studentService.findById(student.getSno());
            msg = Msg.success().add("me",student);
        }

        System.out.println(msg + "ebnenen");
        return msg;
    }

    // 检验是否登录
    @RequestMapping("studentToLogin")
    @ResponseBody
    public Msg studentToUpdatePassAndLogin(HttpSession session){
        student = (Student)  session.getAttribute("student");
        if (student != null) {
            msg = Msg.success();
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 修改密码studentToUpdatePass
    @RequestMapping(value = "/studentToUpdatePass",method = RequestMethod.PUT)
    @ResponseBody
    public Msg studentToUpdatePass(@Param("oldpassword")String oldpassword,
                                   @Param("newpassword")String newpassword,
                                   @Param("confirmpassword")String confirmpassword,
                                   HttpSession session){
        student = (Student)  session.getAttribute("student");
        String stuNo = student.getSno();
        student = studentService.findById(stuNo);
        System.out.println(oldpassword+"a"+newpassword+"b"+confirmpassword+"c");
        if (student != null) {
            if (!confirmpassword.equals(newpassword)){
                msg = Msg.fail().add("updateToNew", "两次输入的密码两次不一样");
            }else{
                if (oldpassword.equals(student.getPassword())){
                    if (newpassword.equals(student.getPassword())){
                        msg = Msg.fail().add("updateToOldAndNew", "修改的密码不能和原本密码一样");
                    }else {
                        studentService.updatePass(oldpassword, newpassword, confirmpassword, stuNo);
                        msg = Msg.success();
                    }
                }else {
                    msg = Msg.fail().add("updateToOld", "输入的旧密码错误");
                }
            }
        }else{
            msg = Msg.fail().add("update", "哦豁用户不存在");
        }
        return msg;
    }




}
